package com.gupao.gupaoeduvippattern.javaBase.thread.day02;

/**
 * @author 大帅
 * @description
 * @date 2020-12-06
 */
public class Queue {

    private int num;

    public Queue(){

    }

    public Queue(int num){
        this.num = num;
    }

    //表示容器中没有数据
    boolean flag = false;

    public synchronized int getNum() {
        if(!flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费：" + num);

        //消费完毕，容器中没有数据
        flag = false;
        notifyAll();

        return num;
    }

    public synchronized void setNum(int num) {
        if(flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("生产：" + num);

        //容器刚生产数据，容器中有数据
        flag = true;
        notifyAll();

        this.num = num;
    }


}
